package com.darren.euc.action;
import com.darren.euc.entity.PageBean;
import com.darren.euc.entity.ResponseJSON;
import com.darren.euc.entity.Role;
import com.darren.euc.service.RoleService;
import com.darren.euc.util.PropertyHelper;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/role")
public class RoleAction {
	
	private static final Logger log = Logger.getLogger(RoleAction.class);
	
	@Autowired
    private RoleService roleService;
		
	
	@ResponseBody
    @RequestMapping(value = "/all",method = RequestMethod.POST)
    public List<Role> getAllRole(HttpServletRequest request,HttpServletResponse response) {
    	List<Role> roles = roleService.getAll();
    	for(Role role:roles){
    		role.setFunctionNames("");
    	}
        return roles;
    }	
	
	@ResponseBody
    @RequestMapping(value = "/list")
    public PageBean getRoleList(HttpServletRequest request,HttpServletResponse response) {
        return roleService.queryForPageUseHQLByRoleName("管理员", PropertyHelper.getPageSize(), 1);
    }

	@ResponseBody
    @RequestMapping(value = "/role")//,method = RequestMethod.POST)
    public Role getRole(HttpServletRequest request,HttpServletResponse response) {
    	List<Role> roles = roleService.getAll();
    	Role role = roles.get(0);
        return role;
    }	
	
	@ResponseBody
    @RequestMapping(value = "/type",method = RequestMethod.POST)
    public List<Role> getRoleType(HttpServletRequest request,HttpServletResponse response) {
		List<Role> roles = roleService.getRoleType();
        return roles;
    }
	
	@ResponseBody
    @RequestMapping(value = "/query",method = RequestMethod.POST)
    public ResponseJSON queryRole(@RequestBody JSONObject requestJson,HttpServletRequest request,HttpServletResponse response) {	
		ResponseJSON responseJson = new ResponseJSON();
		PageBean pageBean = roleService.queryForPageUseHQL(requestJson.getJSONObject("data"), PropertyHelper.getPageSize(), requestJson.getInt("currentPage"));
		responseJson.setData(pageBean);
		return responseJson;
    }
	
	
	@ResponseBody
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public ResponseJSON addRole(@RequestBody Role role, HttpServletRequest request,
			HttpServletResponse response) {
		ResponseJSON responseJson = new ResponseJSON();
		try {
			if(role != null && role.getRoleName() != null){
				List<Role> roles = roleService.getRoleByRoleName(role.getRoleName().trim());
				if(roles != null && roles.size() > 0){
					responseJson.setStatus(-2);
					responseJson.setMessage("角色名已存在！");
				}else{
					role.setCreateTime(new Date());
					role = roleService.save(role);
					responseJson.setStatus(0);
					responseJson.setMessage("添加成功！");
				}
			}else{
				responseJson.setStatus(-3);
				responseJson.setMessage("角色信息错误！");
			}			
			
		} catch (Exception ex) {
			log.info(ex.getMessage(), ex);
			responseJson.setStatus(-1);
			responseJson.setMessage("添加失败！");
		}
		responseJson.setData(role);
		System.out.println("==============================================================");
		System.out.println(responseJson.toString());
		System.out.println("==============================================================");
		return responseJson;
	}
	  
	  
    
    @ResponseBody
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    public ResponseJSON deleteRole(@RequestBody JSONObject requestJson,HttpServletRequest request,HttpServletResponse response) {
    	ResponseJSON responseJson = new ResponseJSON();
    	JSONObject jsonData = new JSONObject();
    	responseJson.setData(jsonData);
    	try{
    		Integer roleId = Integer.parseInt(requestJson.getString("id"));
    		roleService.deleteById(roleId);
        	responseJson.setStatus(0);
    		responseJson.setMessage("删除成功！");
    	}catch(Exception ex){
    		log.info(ex.getMessage(),ex);
        	responseJson.setStatus(-1);
    		responseJson.setMessage("删除失败！");
    	}
    	System.out.println("==============================================================");
    	System.out.println(requestJson.toString());
    	System.out.println(responseJson.toString());
    	System.out.println("==============================================================");
        return responseJson;
    }
    
	@ResponseBody
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public ResponseJSON updateRole(@RequestBody Role role, HttpServletRequest request,
			HttpServletResponse response) {
		ResponseJSON responseJson = new ResponseJSON();
		try {
			if(role != null && role.getRoleName() != null){
				List<Role> roles = roleService.getRoleByRoleNameExcludeCurrentId(role.getRoleName().trim(), role.getId());
				if(roles != null && roles.size() > 0){
					responseJson.setStatus(-2);
					responseJson.setMessage("角色名已存在！");
				}else{
					role.setCreateTime(new Date());
					role = roleService.update(role);
					responseJson.setStatus(0);
					responseJson.setMessage("角色保存成功！");
				}
			}else{
				responseJson.setStatus(-3);
				responseJson.setMessage("角色信息错误！");
			}
		} catch (Exception ex) {
			log.info(ex.getMessage(), ex);
			responseJson.setStatus(-1);
			responseJson.setMessage("保存失败！");
		}
		responseJson.setData(role);
		System.out.println("==============================================================");
		System.out.println(responseJson.toString());
		System.out.println("==============================================================");
		return responseJson;
	}
        
    
}